const cacheObjImpl = sessionStorage;

export function existCache(key: string): boolean {
  return !!cacheObjImpl.getItem(key);
}

export function setCacheObj(key: string, value: object | []) {
  cacheObjImpl.setItem(key, JSON.stringify(value))
}

export function setCache(key: string, value: string) {
  cacheObjImpl.setItem(key, value)
}

export function getCacheObj(key: string, defaultVal?: object | null): any | [] | undefined | null {
  if (!existCache(key)) {
    return defaultVal;
  }
  return JSON.parse(cacheObjImpl.getItem(key) || "{}")
}

export function getCache(key: string, defaultVal?: string | null): string | null | undefined {
  if (!existCache(key)) {
    return defaultVal;
  }
  return cacheObjImpl.getItem(key)
}

export function removeCache(key: string) {
  cacheObjImpl.removeItem(key)
}

export function removeCacheAll() {
  cacheObjImpl.clear()
}

//清理应用程序常规缓存
export function clearAppCache() {
  removeCache('Admin-Token');
  removeCache('currentRoute');
  removeCache('tagsViewData');
  removeCache('menu');
  removeCache('userInfo');
}
